﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TTBusiness.ClassDB;
using TTBusiness.DB_Linq;
using TTBusiness;
namespace TTMarketing.Admin
{
    public partial class ProductAttribute : System.Web.UI.Page
    {
        clsBSProductAttribute objDB = new clsBSProductAttribute();
        protected void Page_Load(object sender, EventArgs e)
        {
            divmessage.InnerHtml = "";
            divmessage2.InnerHtml = "";
        }

        protected void cmbClose_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (Convert.ToInt32(e.Value) == 1)
            {
                plDetail.Visible = true;
            }
            else
            {
                plDetail.Visible = false;
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                tblProduct_Attribute obj = new tblProduct_Attribute();
                obj.ProductID = Convert.ToInt32(this.cmbProduct.SelectedValue);
                obj.Value = this.txtValue.Text;
                obj.CateAttributeID = Convert.ToInt32(hfId.Value);
                tblProduct_Attribute mfc = new tblProduct_Attribute();
                mfc = objDB.getByProductAndCA(Convert.ToInt32(cmbProduct.SelectedValue.ToString()), 
                    Convert.ToInt32(this.hfId.Value));
                if (mfc == null)
                    objDB.insert(obj);
                else
                {
                    mfc.Value = this.txtValue.Text;
                    objDB.update(mfc);
                }
                divmessage.InnerHtml = clsBSMessageAdmin.MessageSuccess("Thành công : ", "Cập nhật thành công").ToString();
                this.rgdGrid.Rebind();
                
            }
            catch (Exception ex)
            {
                divmessage.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi không thể cập nhật : " + ex.Message).ToString();
            }
        }

        protected void rgdGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int attributeid = Convert.ToInt32(rgdGrid.Items[rgdGrid.SelectedIndexes[0]]["ID"].Text);
                this.plDetail.Visible = true;
                clsBSCateAttribute objBSCA = new clsBSCateAttribute();
                this.hfId.Value = objBSCA.getByAttributeAndProduct(attributeid, Convert.ToInt32(cmbProduct.SelectedValue.ToString())).ID.ToString();
                this.cmbClose.SelectedValue = "1";
                this.cmbAttribute.DataBind();
                tblProduct_Attribute mfc = new tblProduct_Attribute();
                mfc = objDB.getByProductAndCA(Convert.ToInt32(cmbProduct.SelectedValue.ToString()), Convert.ToInt32(this.hfId.Value));
                this.txtValue.Text = mfc != null ? mfc.Value.ToString() : " ";
            }
            catch (Exception ex)
            {
                divmessage2.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi : " + ex.Message).ToString();
            }
        }

        protected void cmbProduct_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            try
            {
                this.rgdGrid.Rebind();
            }
            catch (Exception ex)
            {

                divmessage2.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi : " + ex.Message).ToString();
            }
        }

        protected void cmbProduct_DataBound(object sender, EventArgs e)
        {
            try
            {
                this.rgdGrid.Rebind();
            }
            catch (Exception ex)
            {

                divmessage2.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi : " + ex.Message).ToString();
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                this.txtValue.Text = "";
                this.hfId.Value = "";
                this.cmbAttribute.DataBind();
            }
            catch (Exception ex)
            {
                
                divmessage2.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi : " + ex.Message).ToString();
            }
        }
       
    }
}